package com.stone.mythread;

public class ThreadThread extends Thread {

    private static int ticket = 100;

    @Override
    public synchronized void run() {
        while (true) {
            show();
        }
    }

    private static synchronized void show() { // 同步监视器：默认为 当前类.class
        if (ticket > 0) {
            System.err.println(Thread.currentThread().getName() + "：卖票，票号为：" + ticket);
            ticket--;
        }
    }

    public static void main(String[] args) {
        ThreadThread w1 = new ThreadThread();
        ThreadThread w2 = new ThreadThread();
        ThreadThread w3 = new ThreadThread();

        w1.setName("窗口一");
        w2.setName("窗口二");
        w3.setName("窗口三");

        w1.start();
        w2.start();
        w3.start();
    }
}
